var $ = require('jquery');
var http = require('http');
var sys = require("sys");
var fs = require("fs");
var mongodb = require('mongodb');
/*
var options = {
host : 'www.google.com.hk'
//host: 'marshal.easymorse.com'
//port: 80,
//path: '/'
};

var html = '';

http.get(options, function(res) {
res.on('data', function(data) {
html += data;
}).on('end', function() {
var caption = html.match(/<title>.*<\/title>/);
caption = caption[0].replace(/<title>/, '').replace(/<\/title>/, '');
sys.puts(caption);
//var title = $(html).find('title').contents();
//var title = $(html).find('title').text;
//sys.puts(title);
//var dom=$(html);

});
});
*/

var object = null;
var tasks = [];
var cur_url;
function get_caption(url) {
	var newurl = url.replace("http://", '').replace("https://", '').replace("/", '');

		var options = {
			host : newurl
			// host:'www.eygle.com'
		};

		console.log(options.host);

		var html = '';
		console.log("http.get");
		http.get(options, function(res) {
			res.on('data', function(data) {
				html += data;
			}).on('end', function() {
				//*
				var caption = html.match(/<title>.*<\/title>/);
				if (null != caption && caption.length) {
					caption = caption[0].replace(/<title>/, '').replace(/<\/title>/, '');
				}
				console.log(caption);
				// sys.puts(caption);
				new_caption(caption);
				next();
				//var title = $(html).find('title').contents();
				//var title = $(html).find('title').text;
				//sys.puts(title);
				//var dom=$(html);
				//*/
			}).on('error', function(e) {
				console.log('problem with request: ' + e.message);
			});
		}).on('error', function(e) {
			console.log('error------------------------------------------------error');
			next();
		});
		/*
		try {
		} catch(err) {
		console.log("------------------catch");
		console.log("此处发生错误:" + err.description);
		//在此处理错误
		}
		// finally {
		// console.log("finally");
		// // next();
		// }
		//*/
}


var the_collection;
var client = new mongodb.Db('bookmarks', new mongodb.Server("127.0.0.1", 27017, {}), {w: 1});
client.open(function(err, p_client) {
	console.log('open');
	client.collection('urls_with_caption', function (err, collection) {
		collection.insert({a:2}, function(err, docs) { });
		//
		console.log('collection');
		the_collection = collection;

		collection.find().toArray(function(err, results) {
			client.close();
			console.log('close');
		});
	});
});

function start_getting() {
	console.log("start_getting");
	next();
}

function new_caption(caption) {
	console.log("new_caption:" + caption);
	//object[cur_url].caption = caption;
	//console.log(object[cur_url]);
	//var obj = {cur_url:caption};
	var obj = new Object;
	obj.url = cur_url;
	obj.caption = caption;
	//obj[cur_url] = caption;
	console.log(obj);
	//the_collection.insert(obj, function(err, docs) {
	//console.log('insert');
	////console.log(docs);
	//});
	var client = new mongodb.Db('bookmarks', new mongodb.Server("127.0.0.1", 27017, {}), {w: 1});
	client.open(function(err, p_client) {
		console.log('open');
		client.collection('urls_with_caption', function (err, collection) {
			//collection.insert({a:2}, function(err, docs) { });
			collection.insert(obj, function(err, docs) { 
				console.log(err);
				console.log('insert');
			});
			collection.find().toArray(function(err, results) {
				client.close();
				console.log('close');
			});
		});
	});

	//client.collection('urls_with_caption', function (err, collection) {
	//});

}

function next() {
	console.log("next");
	if (tasks.length > 0) {
		cur_url = tasks.pop();
		get_caption(cur_url);
	} else {
		// finish();
	}
}

function finish() {
	console.log("finish");
	// return;
	console.log(object);
	newData = JSON.stringify(object);
	// console.log(newData);
	fs.writeFile('../datas/urls_withCaption.json', newData, function(err) {
		//fs.writeFile('urls_withCaption.json', "abcdefg", function(err) {
		console.log('err:' + err);
		if (err)
			throw err;
		console.log('has finished');
	});
}

function start() {
	fs.readFile("../datas/urls.json", function(err, data) {
		if (err) {
			sys.puts("err");
			throw err;
		}
		sys.puts("File is OK!");
		object = eval('(' + data + ')');

		for (var p in object) {
			if ("count" != p) {
				tasks.push(p);
				//object[p]
			}
		}
		start_getting();
		// finish();
	});
}

function test() {
	var options = {
		host : "vimfox.info"
	};
	console.log(options.host);

	var html = '';

	var req = http.get(options, function(res) {
		res.on('data', function(data) {
			console.log('data');
			html += data;
		}).on('end', function() {
			console.log('end');
			/*
			var caption = html.match(/<title>.*<\/title>/);
			if (null != caption && caption.length) {
			caption = caption[0].replace(/<title>/, '').replace(/<\/title>/, '');
			}
			console.log(caption);
			//*/
		}).on('error', function(e) {
			console.log('error');
			console.log('problem with request: ' + e.message);
		});
	});
	req.on('error', function(e) {
		console.log('Got that pesky error trapped');
	});
}

start();
// test();

